package com.bianmaba.examples.repository;

import com.bianmaba.examples.MainApplication;
import com.bianmaba.examples.bean.authorization.Role;
import com.bianmaba.examples.bean.authorization.User;
import com.bianmaba.examples.repository.authorization.role.RoleRepository;
import com.bianmaba.examples.repository.authorization.user.UserRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = {MainApplication.class})
@Transactional()
public class UserRepositoryTester {
    @Autowired
    private UserRepository repository;
    @Autowired
    private RoleRepository roleRepository;
    private User user1;
    private User user2;

    @Before
    public void before() throws Exception {
        roleRepository.deleteAll();
        repository.deleteAll();
        User user = new User();
        user.setUsername("username");
        user.setPassword("password");
        user.setCreator("creator");


        Role role = new Role();
        role.setName("name1");
        role.setCreator("creator");
        role = roleRepository.save(role);
        user.getRoles().add(role);

        role = new Role();
        role.setName("name2");
        role.setCreator("creator");
        role = roleRepository.save(role);
        user.getRoles().add(role);


        this.user1 = repository.save(user);
        Assert.assertNotNull(this.user1);
        Assert.assertNotNull(this.user1.getId());

        user = new User();
        user.setUsername("username1");
        user.setPassword("password1");
        user.setCreator("creator");

        role = new Role();
        role.setName("name3");
        role.setCreator("creator");
        role = roleRepository.save(role);
        user.getRoles().add(role);

        role = new Role();
        role.setName("name4");
        role.setCreator("creator");
        role = roleRepository.save(role);
        user.getRoles().add(role);


        this.user2 = repository.save(user);
        Assert.assertNotNull(this.user2);
        Assert.assertNotNull(this.user2.getId());
    }


    @Test
    public void executeSqlQuery() {
        List<User> users = repository.executeSqlQuery("select * from system_user", User.class);
        users = repository.executeSqlQuery("select * from system_user", User.class);
        Assert.assertTrue(users.size() == 2);
    }

    @Test
    public void query() {
        Map<String, Object> params = new HashMap<>(0);
        params.put("username_eq", user1.getUsername());
        Page<User> users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 1);

        //条件在多的一端时，不去重复与去重复测试
        params.clear();
        params.put("roles.cdt_gt", user1.getRoles().get(0).getCdt());
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 3);

        users = repository.findAllDistinct(params, null);
        Assert.assertTrue(users.getContent().size() == 2);


        //in条件查询
        params.clear();
        params.put("id_in", new String[]{user1.getId(), user2.getId()});
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 2);

        //notin条件查询
        params.clear();
        params.put("id_notin", new String[]{user1.getId(), user2.getId()});
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 0);

        //notin条件查询
        params.clear();
        params.put("cdt_gt", 1);
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 2);

        //lt条件查询
        params.clear();
        params.put("cdt_lt", Long.MAX_VALUE);
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 2);


        //ne条件查询
        params.clear();
        params.put("cdt_ne", user1.getCdt());
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 1);

        //le条件查询
        params.clear();
        params.put("cdt_le", user1.getCdt());
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 1);

        //ge条件查询
        params.clear();
        params.put("cdt_ge", user2.getCdt());
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 1);

        //like条件查询
        params.clear();
        params.put("username_like", "ser");
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 2);

        //like条件查询
        params.clear();
        params.put("username_:like", "1");
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 1);

        //isnull
        params.clear();
        params.put("username_isnull", "");
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 0);
        //isnotnull
        params.clear();
        params.put("username_isnotnull", "");
        users = repository.findAll(params, null);
        Assert.assertTrue(users.getContent().size() == 2);
    }
}
